﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

namespace Epitech.Krisk.Xna.UI.Game.InGame.Gameplay
{
    public class Continent
    {
        public static Dictionary<String, Continent> Continents { get; private set; }

        private static bool _init = false;

        private static void MakeNeighbor(String terr, params String[] neighbors)
        {
            Territory from = null;

            List<Territory> nt = (from neighbor in neighbors from continent in Continents from territory in continent.Value.Territories where territory.Name.ToLower() == neighbor.ToLower() select territory).ToList();

            foreach (var continent in Continents)
            {
                foreach (var territory in continent.Value.Territories)
                {
                    if (territory.Name.ToLower() == terr.ToLower())
                        from = territory;
                }
            }

            if (from != null && nt.Count == neighbors.Length)
            {
                foreach (var territory in nt)
                {
                    if (from.Neighbors == null)
                        from.Neighbors = new List<Territory>();
                    from.Neighbors.Add(territory);
                }
            }
            else
            {
                throw new Exception();
            }
        }

        public static void CreateContinentAndTerritory()
        {
            if (_init)
                return;
            Continents = new Dictionary<String, Continent>();

            #region Afrique
            
            Continent Afrique = new Continent();
            Afrique.Name = "Afrique";
            Afrique.Territories = new List<Territory>();
            Territory tmp;

            tmp = new Territory(new Color(165, 42, 42), new Vector2(2030, 1394)) {Name = "Afrique du Nord", Continent = Afrique};
            Afrique.Territories.Add(tmp);
            tmp = new Territory(new Color(184, 134, 11), new Vector2(2390, 1530)) {Name = "Afrique Orientale", Continent = Afrique };
            Afrique.Territories.Add(tmp);
            tmp = new Territory(new Color(128, 0 , 0), new Vector2(2275, 1950)) {Name = "Afrique du Sud", Continent = Afrique };
            Afrique.Territories.Add(tmp);
            tmp = new Territory(new Color(160, 82, 45), new Vector2(2234, 1714)) {Name = "Congo", Continent = Afrique };
            Afrique.Territories.Add(tmp);
            tmp = new Territory(new Color(210, 105, 30), new Vector2(2250, 1325)) {Name = "Egypte", Continent = Afrique };
            Afrique.Territories.Add(tmp);
            tmp = new Territory(new Color(222, 184, 135), new Vector2(2540, 1833)) {Name = "Madagascar", Continent = Afrique };
            Afrique.Territories.Add(tmp);

            Continents.Add(Afrique.Name, Afrique);

            #endregion

            #region Amerique du Nord
            
            Continent AmeriqueNord = new Continent();
            AmeriqueNord.Name = "Amerique du Nord";
            AmeriqueNord.Territories = new List<Territory>();

            tmp = new Territory(new Color(255, 239, 213), new Vector2(372, 645)) {Name = "Alaska", Continent = AmeriqueNord};
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 218, 185), new Vector2(723, 962)) {Name = "Alberta", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 228, 181), new Vector2(1027, 1219)) {Name = "Etats de l'Est", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 127, 80), new Vector2(740, 1134)) {Name = "Etats de l'Ouest", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 215, 0), new Vector2(752, 761)) {Name = "Territoires du Grand-Ouest", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 99, 71), new Vector2(1550, 498)) {Name = "Groenland", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(240, 230, 140), new Vector2(885, 1395)) {Name = "Mexique", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 140, 0), new Vector2(971, 943)) {Name = "Ontario", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 165, 0), new Vector2(1225, 908)) {Name = "Quebec", Continent = AmeriqueNord };
            AmeriqueNord.Territories.Add(tmp);


            Continents.Add(AmeriqueNord.Name, AmeriqueNord);

            #endregion

            #region Amerique du Sud
            
            Continent AmeriqueSud = new Continent();
            AmeriqueSud.Name = "Amerique du Sud";
            AmeriqueSud.Territories = new List<Territory>();

            tmp = new Territory(new Color(255, 160, 122), new Vector2(1228, 2037)) {Name = "Argentine", Continent = AmeriqueSud };
            AmeriqueSud.Territories.Add(tmp);
            tmp = new Territory(new Color(240, 128, 128), new Vector2(1422, 1707)) {Name = "Bresil", Continent = AmeriqueSud };
            AmeriqueSud.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 0, 0), new Vector2(1212, 1805)) {Name = "Perou", Continent = AmeriqueSud };
            AmeriqueSud.Territories.Add(tmp);
            tmp = new Territory(new Color(178, 34, 34), new Vector2(1175, 1560)) {Name = "Venezuela", Continent = AmeriqueSud };
            AmeriqueSud.Territories.Add(tmp);

            Continents.Add(AmeriqueSud.Name, AmeriqueSud);

            #endregion

            #region Oceanie

            Continent Oceanie = new Continent();
            Oceanie.Name = "Oceanie";
            Oceanie.Territories = new List<Territory>();

            tmp = new Territory(new Color(75, 0, 130), new Vector2(3400, 1950)) {Name = "Australie Occidentale", Continent = Oceanie };
            Oceanie.Territories.Add(tmp);
            tmp = new Territory(new Color(255, 51, 255), new Vector2(3300, 1585)) {Name = "Borneo", Continent = Oceanie };
            Oceanie.Territories.Add(tmp);
            tmp = new Territory(new Color(123, 104, 238), new Vector2(3635, 1900)) {Name = "Nouvelle Galles du Sud", Continent = Oceanie };
            Oceanie.Territories.Add(tmp);
            tmp = new Territory(new Color(148, 0, 211), new Vector2(3535, 1670)) {Name = "Nouvelle Guinee", Continent = Oceanie };
            Oceanie.Territories.Add(tmp);

            Continents.Add(Oceanie.Name, Oceanie);

            #endregion
            
            #region Asie

            Continent Asie = new Continent();
            Asie.Name = "Asie";
            Asie.Territories = new List<Territory>();

            tmp = new Territory(new Color(152, 251, 152), new Vector2(2874, 1300)) {Name = "Indes", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(0, 100, 0), new Vector2(3550, 1160)) {Name = "Japon", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(143, 188, 143), new Vector2(2700, 1080)) {Name = "Kazakstan", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(127, 255, 0), new Vector2(3800, 750)) {Name = "Khabarovsk", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(0, 128, 0), new Vector2(3340, 1040)) {Name = "Mongolie", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(0, 255, 0), new Vector2(2500, 1250)) {Name = "Moyen-Orient", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(128, 128, 0), new Vector2(2830, 840)) {Name = "Oural", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(107, 142, 35), new Vector2(3090, 1400)) {Name = "Siam", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(60, 179, 113), new Vector2(3000, 670)) {Name = "Siberie", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(46, 139, 87), new Vector2(3220, 800)) {Name = "Tchita", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(154, 205, 50), new Vector2(3160, 1250)) {Name = "Tibet", Continent = Asie };
            Asie.Territories.Add(tmp);
            tmp = new Territory(new Color(50, 205, 50), new Vector2(3440, 650)) {Name = "Yakoutie", Continent = Asie };
            Asie.Territories.Add(tmp);

            Continents.Add(Asie.Name, Asie);

            #endregion

            #region Europe

            Continent Europe = new Continent();
            Europe.Name = "Europe";
            Europe.Territories = new List<Territory>();

            tmp = new Territory(new Color(70, 130, 180), new Vector2(2120, 945)) {Name = "Europe du Nord", Continent = Europe };
            Europe.Territories.Add(tmp);
            tmp = new Territory(new Color(65, 105, 225), new Vector2(2015, 1100)) {Name = "Europe Occidentale", Continent = Europe };
            Europe.Territories.Add(tmp);
            tmp = new Territory(new Color(30, 144, 255), new Vector2(2218, 1110)) {Name = "Europe du Sud", Continent = Europe };
            Europe.Territories.Add(tmp);
            tmp = new Territory(new Color(0, 0, 205), new Vector2(1971, 924)) {Name = "Grande Bretagne", Continent = Europe };
            Europe.Territories.Add(tmp);
            tmp = new Territory(new Color(176, 224, 230), new Vector2(1800, 710)) {Name = "Islande", Continent = Europe };
            Europe.Territories.Add(tmp);
            tmp = new Territory(new Color(0, 255, 255), new Vector2(2200, 673)) {Name = "Scandinavie", Continent = Europe };
            Europe.Territories.Add(tmp);
            tmp = new Territory(new Color(0, 0, 128), new Vector2(2475, 849)) {Name = "Ukraine", Continent = Europe };
            Europe.Territories.Add(tmp);

            Continents.Add(Europe.Name, Europe);

            #endregion

            #region Territoire Voisin

            #region Afrique

            MakeNeighbor("Afrique du Nord", "Bresil", "Egypte", "Congo", "Afrique Orientale", "Europe Occidentale", "Europe du Sud");
            MakeNeighbor("Afrique Orientale", "Egypte", "Congo", "Afrique du Nord", "Madagascar", "Afrique du Sud");
            MakeNeighbor("Afrique du Sud", "Congo", "Madagascar", "Afrique Orientale");
            MakeNeighbor("Congo", "Afrique du Sud", "Afrique du Nord", "Afrique Orientale");
            MakeNeighbor("Egypte", "Afrique du Nord", "Afrique Orientale", "Europe du Sud", "Moyen-Orient");
            MakeNeighbor("Madagascar", "Afrique du Sud", "Afrique Orientale");

            #endregion

            #region Amerique du Nord

            MakeNeighbor("Alaska", "Khabarovsk", "Alberta", "Territoires du Grand-Ouest");
            MakeNeighbor("Alberta", "Alaska", "Etats de l'Ouest", "Territoires du Grand-Ouest", "Ontario");
            MakeNeighbor("Etats de l'Est", "Mexique", "Etats de l'Ouest", "Quebec", "Ontario");
            MakeNeighbor("Etats de l'Ouest", "Mexique", "Etats de l'Est", "Alberta", "Ontario");
            MakeNeighbor("Territoires du Grand-Ouest", "Alaska", "Groenland", "Alberta", "Ontario");
            MakeNeighbor("Groenland", "Territoires du Grand-Ouest", "Quebec", "Islande");
            MakeNeighbor("Mexique", "Etats de l'Est", "Etats de l'Ouest", "Venezuela");
            MakeNeighbor("Ontario", "Etats de l'Est", "Etats de l'Ouest", "Groenland", "Territoires du Grand-Ouest", "Quebec", "Alberta");
            MakeNeighbor("Quebec", "Etats de l'Est", "Groenland", "Ontario");

            #endregion

            #region Amerique du Sud

            MakeNeighbor("Argentine", "Bresil", "Perou");
            MakeNeighbor("Bresil", "Argentine", "Perou", "Venezuela", "Afrique du Nord");
            MakeNeighbor("Perou", "Argentine", "Bresil", "Venezuela");
            MakeNeighbor("Venezuela", "Perou", "Bresil", "Mexique");

            #endregion

            #region Oceanie

            MakeNeighbor("Australie Occidentale", "Borneo", "Nouvelle Galles du Sud", "Nouvelle Guinee");
            MakeNeighbor("Borneo", "Australie Occidentale", "Siam", "Nouvelle Guinee");
            MakeNeighbor("Nouvelle Galles du Sud", "Australie Occidentale", "Nouvelle Guinee");
            MakeNeighbor("Nouvelle Guinee", "Australie Occidentale", "Nouvelle Galles du Sud", "Borneo");

            #endregion

            #region Asie

            MakeNeighbor("Indes", "Moyen-Orient", "Kazakstan", "Tibet", "Siam");
            MakeNeighbor("Japon", "Khabarovsk", "Mongolie");
            MakeNeighbor("Kazakstan", "Moyen-Orient", "Tibet", "Indes", "Oural", "Ukraine");
            MakeNeighbor("Khabarovsk", "Japon", "Yakoutie", "Tchita", "Mongolie", "Alaska");
            MakeNeighbor("Tibet", "Siberie", "Mongolie", "Oural", "Kazakstan", "Indes", "Siam");
            MakeNeighbor("Moyen-Orient", "Egypte", "Ukraine", "Kazakstan", "Indes");
            MakeNeighbor("Oural", "Tibet", "Ukraine", "Kazakstan", "Siberie");
            MakeNeighbor("Siam", "Borneo", "Tibet", "Indes");
            MakeNeighbor("Siberie", "Oural", "Tibet", "Mongolie", "Tchita", "Yakoutie");
            MakeNeighbor("Tchita", "Siberie", "Khabarovsk", "Mongolie", "Yakoutie");
            MakeNeighbor("Mongolie", "Tibet", "Khabarovsk", "Tchita", "Japon", "Siberie");
            MakeNeighbor("Yakoutie", "Khabarovsk", "Tchita", "Siberie");

            #endregion

            #region Europe

            MakeNeighbor("Europe du Nord", "Europe du Sud", "Ukraine", "Scandinavie", "Grande Bretagne", "Europe Occidentale");
            MakeNeighbor("Europe Occidentale", "Europe du Sud", "Afrique du Nord", "Grande Bretagne", "Europe du Nord");
            MakeNeighbor("Europe du Sud", "Europe Occidentale", "Afrique du Nord", "Egypte", "Europe du Nord", "Ukraine");
            MakeNeighbor("Grande Bretagne", "Europe Occidentale", "Islande", "Scandinavie", "Europe du Nord");
            MakeNeighbor("Islande", "Grande Bretagne", "Groenland", "Scandinavie");
            MakeNeighbor("Scandinavie", "Grande Bretagne", "Islande", "Europe du Nord", "Ukraine");
            MakeNeighbor("Ukraine", "Oural", "Europe du Sud", "Europe du Nord", "Scandinavie", "Kazakstan", "Moyen-Orient");

            #endregion

            #endregion
            
        }

        public String Name { get; set; }
        public List<Territory> Territories { get; set; }
    }
}
